/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

/**
 *
 * @author Gjun
 */
@WebServlet(name = "carlist", urlPatterns = {"/carlist"})
public class carlist extends HttpServlet {
String id;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet carlist</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet carlist at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        response.sendRedirect("infor.jsp");
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
            //processRequest(request, response);
            HttpSession session=request.getSession();
            id=(String)session.getAttribute("product_id");
             int tempid=(Integer.valueOf(id)+1);
                    id=""+tempid;
            System.out.println("0000");
            if (session.getAttribute("person")==null){
               // JOptionPane.showMessageDialog(null,"請先登入");
                System.out.println("11111122233");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
            else{
                try{
                    String namesql = "Select * from product where product_id="+"'"+id+"'";
                    ArrayList<HashMap> rslist = new DAO().query(namesql, new String[]{});
                    HashMap map=rslist.get(0);
                    System.out.println("11");
                    ArrayList list = new ArrayList();
                    if(session.getAttribute("carlist")!=null){
                        list=(ArrayList)session.getAttribute("carlist");
                    }
                    System.out.println("12");
                    list.add(map);
                    session.setAttribute("carlist", list);
                   
                   // request.setAttribute("product_id", id);
                    System.out.println("13");
                    request.getRequestDispatcher("infor.jsp").forward(request, response);
                    }catch(Exception e){
                         System.out.println("14");
                        request.getRequestDispatcher("testt.jsp").forward(request, response);
                    }
            }                 
    
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    
   
}
